package cn.com.api.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.api.entity.ApiT_member;
import cn.com.api.entity.ApiTmember;
import cn.com.api.entity.ApiU_user_friend_group;
import cn.com.api.entity.ApiU_user_friend_group_detail;
import cn.com.api.entity.MemberObject;

import cn.com.api.mapper.ApiTmemberMapper;
import cn.com.api.mapper.ApiU_user_friend_groupMapper;
import cn.com.api.mapper.ApiU_user_friend_group_detailMapper;

import cn.com.api.service.ApiU_user_friend_groupService;
import cn.com.api.service.impl.base.U_user_friend_groupServiceImplBase;
import cn.com.util.PageInfo;
import cn.com.util.Util;

/**
 * 
 **/
@Service
public class ApiU_user_friend_groupServiceImpl extends U_user_friend_groupServiceImplBase implements ApiU_user_friend_groupService{
	@Autowired
	private ApiU_user_friend_groupMapper apiU_user_friend_groupMapper;
	@Autowired
	private ApiU_user_friend_group_detailMapper apiU_user_friend_group_detailMapper;
	@Autowired
	private ApiTmemberMapper apiTmemberMapper;
	
     	//新增对象后返回记录ID
    public int U_user_friend_groupInsertion(ApiU_user_friend_group u_user_friend_group){
        return this.apiU_user_friend_groupMapper.U_user_friend_groupInsertion(u_user_friend_group);
    }
 
    public List<ApiU_user_friend_group> getfriend_groups(Map map, PageInfo page)
    {
    	return this.apiU_user_friend_groupMapper.findU_user_friend_groupsByMap(map, page);
    }
    
    //删除分组
    public int delfriend_group(String group_id, String home_group_id)
    {
//    	int ret = this.apiU_user_friend_group_detailMapper.delU_user_friend_group_detailsByGroup_id(group_id);
    	List<ApiU_user_friend_group_detail> details = this.apiU_user_friend_group_detailMapper.findU_user_friend_group_detailsByGroupID(group_id);
    	for (ApiU_user_friend_group_detail detail : details) {
			detail.setFriend_group_id(home_group_id);
			detail.setAdd_time(Util.CurrentTime());
			this.apiU_user_friend_group_detailMapper.updateU_user_friend_group_detail(detail);
		}
//    	if(ret >0)
//    	{
//    		ret = this.apiU_user_friend_groupMapper.deleteU_user_friend_group(Integer.valueOf(group_id));
//    	}
    	int ret = this.apiU_user_friend_groupMapper.deleteU_user_friend_group(Integer.valueOf(group_id));
    	return ret;
    }
    
    //查询通讯录注册用户信息
	public List<MemberObject> searchUserFromCommunicationList(String mobile_numbers)
	{
		List<MemberObject> member_list = new ArrayList<MemberObject>();
		String numbers[] = mobile_numbers.split(",");
		for(String num : numbers)
		{
			MemberObject mObject = new MemberObject();
			ApiTmember tMember = this.apiTmemberMapper.findMemberByCuserid(num);
			if(!Util.isNull(tMember))
			{
				mObject.setMember_id(tMember.getCid());
				if(!Util.isNull(tMember.getCavatar()) && tMember.getCavatar().length()>0)
				{
					mObject.setHead_img(tMember.getCavatar()+"?imageView2/1/w/100/h/100");
				}
				mObject.setMember_name(tMember.getCnick());
				member_list.add(mObject);
			}
		}		
		return member_list;
	}
	
	//获取分组信息by user_id
	public ApiU_user_friend_group getUserFriendGroup(String user_id, String group_name)
	{
		return this.apiU_user_friend_groupMapper.findFriend_groupByName(user_id, group_name);
	}
	
	//查询用户分组sort最大值by userid
	public ApiU_user_friend_group getMaxSortUserFriendGroup(String user_id)
	{
		return this.apiU_user_friend_groupMapper.findMaxFriend_groupByUserId(user_id);
	}
}


